package com.ibeeking.found.upms.g.rest.controller.v1;

import com.ibeeking.found.common.entity.GlobalIndustryDTO;
import com.ibeeking.found.upms.g.rest.service.IGmcPermissionService;
import com.ibeeking.found.common.annotation.LoginUser;
import com.ibeeking.found.common.constants.HeaderNounConstant;
import com.ibeeking.found.common.entity.CurrentUser;
import com.ibeeking.found.upms.g.rest.service.IGmcPermissionResourceService;
import com.ibeeking.found.upms.g.rest.service.IGmcRolePermissionService;
import com.ibeeking.found.upms.service.common.bo.PermissionResourceBO;
import com.ibeeking.found.upms.service.common.bo.RolePermissionBO;
import com.ibeeking.found.upms.service.common.param.PermissionCopyParam;
import com.ibeeking.found.upms.service.common.param.PermissionParam;
import com.ibeeking.found.upms.service.common.query.PermissionQuery;
import com.ibeeking.found.upms.service.common.vo.CurrentPermissionVO;
import com.ibeeking.found.upms.service.common.vo.FrontEndTypeVO;
import com.ibeeking.found.upms.service.common.vo.PermissionTreeVO;
import com.ibeeking.found.upms.service.common.vo.PermissionVO;
import com.ibeeking.nematos.constants.enums.OperateLogTypeEnum;
import com.ibeeking.nematos.log.annotation.Log;
import com.ibeeking.nematos.log.utils.LogUtils;
import com.ibeeking.nematos.utils.base.BaseValidationGroup;
import com.ibeeking.nematos.utils.json.JsonUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName PermissionController控制器
 * @Description
 * @Author ibeeking
 * @Date 2020-12-01 18:15
 **/
@RestController
@RequestMapping("/g/permission/v1")
public class GmcPermissionController {

    @Resource
    private IGmcPermissionService permissionServiceImpl;
    @Resource
    private IGmcRolePermissionService rolePermissionServiceImpl;
    @Resource
    private IGmcPermissionResourceService permissionResourceServiceImpl;

    //@Log(logType = OperateLogTypeEnum.QUERY, describe = "查询权限菜单树")
    @PostMapping(value = "/queryList")
    public List<PermissionTreeVO> queryList(@RequestBody @Validated PermissionQuery permissionQuery) {
        return permissionServiceImpl.queryList(permissionQuery);
    }
    // 利用cache解决菜单查询显示太慢的问题
    @PostMapping(value = "/queryListCache")
    public List<PermissionTreeVO> queryListCache(@RequestBody @Validated PermissionQuery permissionQuery) {
        return permissionServiceImpl.queryListCache(permissionQuery);
    }

    @Log(logType = OperateLogTypeEnum.SAVE, describe = "保存权限菜单")
    @PostMapping(value = "/save")
    public Boolean add(@LoginUser CurrentUser currentUser, @RequestBody @Validated(BaseValidationGroup.Insert.class) PermissionParam permissionParam) {
        permissionParam.setCreateBy(currentUser.getId());
        return permissionServiceImpl.add(permissionParam);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "修改权限菜单")
    @PostMapping(value = "/modify")
    public Boolean modify(@LoginUser CurrentUser currentUser, @RequestBody @Validated(BaseValidationGroup.Update.class) PermissionParam permissionParam) {
        permissionParam.setModifyBy(currentUser.getId());
        return permissionServiceImpl.modify(permissionParam);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询权限菜单")
    @GetMapping(value = "/query/{id}")
    public PermissionVO view(@PathVariable("id") Long id) {
        return permissionServiceImpl.view(id);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "修改权限菜单状态")
    @GetMapping(value = "/modifyStatus/{id}/{status}")
    public Boolean changeStatus(@LoginUser CurrentUser currentUser, @PathVariable("id") Long id, @PathVariable("status") Integer status) {
        return permissionServiceImpl.changeStatus(id, status, currentUser.getId());
    }

    @Log(logType = OperateLogTypeEnum.DEL, describe = "删除权限菜单")
    @GetMapping(value = "/del/{id}")
    public Boolean del(@PathVariable("id") Long id) {
        return permissionServiceImpl.del(id);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询有效权限菜单树")
    @GetMapping(value = "/queryEffectiveList")
    public List<CurrentPermissionVO> queryEffectiveList(@LoginUser(isFull = true) CurrentUser currentUser, HttpServletRequest request) {
        return permissionServiceImpl.queryEffectiveList(currentUser, request.getHeader(HeaderNounConstant.JWT_JTI));
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "根据权限id查询角色id列表")
    @GetMapping(value = "/queryRoleIdsByPermissionId/{permissionId}")
    public List<String> queryRoleIdsByPermissionId(@PathVariable("permissionId") @NotNull(message = "permissionId不能为空") Long permissionId) {
        List<RolePermissionBO> rolePermissionBos = rolePermissionServiceImpl.queryRolePermissionByPermissionId(permissionId);
        return rolePermissionBos.parallelStream().map(rolePermissionBO -> String.valueOf(rolePermissionBO.getRoleId())).distinct().collect(Collectors.toList());
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "根据权限id查询资源id列表")
    @GetMapping(value = "/queryResourceIdsByPermissionId/{permissionId}")
    public List<String> queryResourceIdsByPermissionId(@PathVariable("permissionId") @NotNull(message = "permissionId不能为空") Long permissionId) {
        List<PermissionResourceBO> permissionResourceBos = permissionResourceServiceImpl.queryPermissionResourceByPermissionId(permissionId);
        return permissionResourceBos.parallelStream().map(rolePermissionBO -> String.valueOf(rolePermissionBO.getResourceId())).distinct().collect(Collectors.toList());
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询行业数据库列表")
    @PostMapping(value = "/queryAllGlobalIndustry/{industry}/{frontend}")
    public List<GlobalIndustryDTO> queryAllGlobalIndustry(@PathVariable("industry") String industry, @PathVariable("frontend") String frontend) {
        return permissionServiceImpl.queryAllGlobalIndustry(industry, frontend);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询终端可见性")
    @GetMapping(value = "/queryFrontEndValid/{industry}")
    public FrontEndTypeVO queryFrontEndValid(@PathVariable("industry") String industry) {
        return permissionServiceImpl.queryFrontEndValid(industry);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "菜单复制")
    @PostMapping(value = "/copyFromIndustry")
    public Boolean copyFromIndustry(@RequestBody @Validated PermissionCopyParam param) {
        return permissionServiceImpl.copyFromIndustry(param);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "清除菜单")
    @PostMapping(value = "/clearMenu/{industry}/{frontend}")
    public Boolean clearMenuByIndustry(@PathVariable("industry") String industry, @PathVariable("frontend") String frontend) {
        return permissionServiceImpl.clearMenuByIndustry(industry, frontend);
    }

}